#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "User.h"
#include "CurrentUser.h"
#include "UserTable.h"

#include <QPushButton>
#include <QSqlError>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("登陆界面");
    this->resize(798,646);

    //跳转到注册界面
    connect(ui->reg_btn,&QPushButton::clicked,[=](){
        emit showRegWindow();   //发送显示注册界面信号
        this->hide();
    });

    //跳转到欢迎界面
    connect(ui->login_btn,&QPushButton::clicked,[=](){
       QString account = ui->uid_et->toPlainText();
       QString password = ui->password_et->toPlainText();
       //当前用户表current_user
       CurrentUser current_user;
       //默认用户(买家)
       if(account == "ichen" && password == "123"){
           User u;
           u.name = "ichen教教徒";
           u.account = "ichen";
           u.password = "123";
           u.phone_num = "10086";
           u.category = 0;

           current_user.insertCurrentUser(u);

           emit showWelcomWindow();
           this->hide();
       }
       //默认用户(卖家)
       if(account == "admin" && password == "123"){
           User u;
           u.name = "管理员";
           u.account = "admin";
           u.password = "123";
           u.phone_num = "10086";
           u.category = 1;

           current_user.insertCurrentUser(u);

           emit showAdminMainWindow();
           this->hide();
       }
       //普通用户登陆
       else{
           UserTable user_table;
           User *u = new User[500];
           User cu; //当前用户
           u = user_table.queryAllUser();
           QString warning = "";    //警告信息
           //遍历判断是否存在该账户
           int i = 0;
           bool flag = false;   //1表示存在,0表示不存在
           while(u[i].name != ""){
               if(u[i].account == ui->uid_et->toPlainText() && u[i].password == ui->password_et->toPlainText()){
                   flag = true;
                   cu.name = u[i].name;
                   cu.category = u[i].category;
                   cu.account = u[i].account;
                   cu.password = u[i].password;
                   cu.phone_num = u[i].phone_num;
                   break;
               }
               i++;
           }
           if(flag == false){
               warning += "账户或者密码错误";
               ui->warning_text->setText(warning);
           }else{
               current_user.insertCurrentUser(cu);
               if(cu.category == 0) emit showWelcomWindow();    //普通账户
               else if(cu.category == 1) emit showAdminMainWindow();    //管理员账户
               this->hide();
           }
       }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

//回到登陆界面
void MainWindow::replyBackToLogin(){
    this->show();
    //删除刚刚登陆的账号
    CurrentUser current_user;
    current_user.deleteCurrentUser();
}


